<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>

<html>
<head>
    <s:set name="isNew" value="id == null"/>
    <s:set var="title">
        <s:if test="#isNew">创建</s:if><s:else>修改</s:else>部门
    </s:set>

    <title><s:property value="#title"/></title>
    <style type="text/css">
        .tree {
            display: inline;
            float: left;
            margin-left: 10px;
            margin-right: 10px
        }
    </style>
    <script type="text/javascript" src="${ctx}/js/jstree/jquery.jstree.js"></script>
    <script type="text/javascript" src="${ctx}/js/groupTree.js"></script>
    <script>
        $(document).ready(function() {
            //聚焦第一个输入框
            $("#name").focus();
            //为inputForm注册validate函数
            $("#inputForm").validate({
                                         // update by sevenSpend 2011-11-26 start -->
                                         rules:{
                                             name: {
                                                 required: true
                                             }
                                         }
                                         // <-- update by sevenSpend 2011-11-26 end
                                     });

            $('#submitButton').click(function() {
                getCheckList();
                $("#inputForm").submit();
            });

            initTree();
            initAuthorTree(); // add by sevenSpend 2011/11/24
                        
    		// add by sevenSpend 2011-12-01 start -->
		   	$("#category").focus(function(){
		   		var id = document.getElementById("id");
		   		if (id.value != "") {
		       		$(this).attr('defaultIndex', $(this).attr('selectedIndex'));
				}
		   	});
		   	
		   	$("#category").change(function(){
		   		var id = document.getElementById("id");
		   		if (id.value != "") {
		       		$(this).attr('selectedIndex', $(this).attr('defaultIndex'));  
				} 
		   	});
    		// <-- add by sevenSpend 2011-12-01 end		   	
        });

        function initTree() {
            var updateURL = "/security/authority!updateDept.action";
            var menuTree = initGroupTree(${deptJson}, '${ctx}', updateURL);
            menuTree.bind("select_node.jstree", function (event, data) {
            })
        }
        // add by sevenSpend 2011/11/24 -->
        function initAuthorTree() {
            var updateURL = "/security/authority!updateDept.action";

            var authorTree = initGroupTreeCheckbox(${authorJson}, '${ctx}', updateURL, 'treeAuthor');
            var checkedAuthIds = $("#checkedAuthIdsStr").val().split(",");

            authorTree.bind("loaded.jstree", function (event, data) {
            	$("#treeAuthor").jstree("open_all");
				$("#treeAuthor").find("li").each(function() {
					for (var i = 0; i < checkedAuthIds.length; i++) {
						if ($(this).attr("id") == checkedAuthIds[i]) {
							$("#treeAuthor").jstree("check_node", $(this));
							break;
						}
					}
				});
            });
        }
        
        function getCheckList(obj) {
			var ids = getCheckId(obj);
			var strIds = "";
			for (var i=0; i<ids.length; i++) {
				strIds += ids[i];
				strIds += ",";
			}

			if (strIds == "") {
				$("#checkedAuthIdsStr").val("");
				return;
			}
			
			$("#checkedAuthIdsStr").val(strIds.substr(0, strIds.length-1));
		}

        function getCheckId(obj) {
			var ids =[];
			$.jstree._reference($("#treeAuthor")).get_checked(obj).each(function(i, n) {
				ids.push(n.id);
				if ($(this).find("> ul").length > 0) {
					ids.push(getCheckId($(this)));
				}
			});
			return ids;
		}
        // add by sevenSpend 2011/11/24 <--
    	// add by sevenSpend 2011-11-26 start -->
        jQuery.validator.addMethod("parentDeptCheck", function(value, element) {
        	var category = $("#category").val();
        	var parentId = $("#parentId").val();
		    
			var parentDeptCategory = "";
			if (document.getElementById(parentId)) {
				parentDeptCategory = document.getElementById(parentId).value;
			}
		
		    return (category == "0" && parentDeptCategory == "") || 
		           (category == "1" && parentDeptCategory == "0") || 
		           (category == "2" && parentDeptCategory == "1") }, "请根据部门类别选择合适的上级部门");
    	// <-- add by sevenSpend 2011-11-26 end
    </script>
    <meta name="menu" content="deptManage"/>
</head>

<body>

<div class="jquery_tab">

    <div class="content_block">
        <h2 class="jquery_tab_title"><s:property value="#title"/></h2>

        <form id="inputForm" action="dept!save.action" method="post">
            <input type="hidden" name="id" value="${id}"/>
            <input type="hidden" name="revision" value="${revision}"/>
            <!-- add by sevenSpend 2011-11-27 start -->
            <input type="hidden" name="orginDeptId" value="${deptId}"/>
            <!-- add by sevenSpend 2011-11-27 end -->

            <!-- add by sevenSpend 2011-11-26 start -->
            <p>
                <label for="name">部门类别:</label>
                <code:dispCodeList codeType="DEPT_CATEGORY" name="category"   id="category"  codeValue="${category}" displayHeader="true"/>
            </p>
            <!-- add by sevenSpend 2011-11-26 end -->

            <p>
                <label for="name">上级部门:</label>
                <!-- update by sevenSpend 2011-11-26 start -->
                <s:select emptyOption ="true" list="#request.parentDeptList" listKey="id" listValue="name" id="parentId" name="parentId"/>
                <!-- update by sevenSpend 2011-11-26 end -->
            </p>

            <p>
                <label for="name">部门名:</label>
                <input type="text" id="name" name="name" value="${name}" class="input-small"/>
            </p>

            <p>
                <label for="treeAuthor">部门权限:</label>
                <!-- update by sevenSpend 2011/11/24-->
        		<div id="treeAuthor" style="width: 400px"></div>
        		<input type="hidden" id="checkedAuthIdsStr" name="checkedAuthIdsStr" value="${checkedAuthIdsStr}">
                <!-- update by sevenSpend 2011/11/24-->
            </p>

            <p>
            <!-- update by sevenSpend 2011/11/27 start -->
                <!-- <a class="button" href="#" id="submitButton"><span>提交</span></a> -->
                <a class="button" href="#" id="submitButton" ><span>提交</span></a>
            <!-- update by sevenSpend 2011/11/27 end -->
                <a class="button" onclick="javascript:window.location.href='dept.action'"><span>返回</span></a>
            </p>
        </form>
    </div>
</div>
<div class="jquery_tab">
    <div class="content_block">
        <h2 class="jquery_tab_title">部门组织架构</h2>

        <div id="treeMenu" class="tree" style="width: 400px"></div>
    </div>
</div>
</body>
</html>
